<template>
  <div class="detail-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >

      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.color">
        <el-button v-loading="loading" type="success" @click="submitForm">
          {{ isEdit ? '更新' : '保存' }}
        </el-button>
      </sticky>

      <div class="app-container" style="padding: 10px 20px">
        <warn />

        <el-row>
          <el-col :span="22">
            <el-form-item label="ID" v-if="isEdit">
              <el-input v-model="postForm.id" disabled style="width: 200px" />
            </el-form-item>
            <el-form-item label="分类" prop="type">
              <el-select
                v-model="postForm.type"
                placeholder="分类"
                class="filter-item"
                filterable
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="店名">
              <el-input v-model="postForm.name" style="width: 200px" />
            </el-form-item>
            <el-form-item label="电话" prop="mobile">
              <el-input v-model="postForm.mobile" style="width: 200px" />
            </el-form-item>
            <el-form-item label="姓名">
              <el-input v-model="postForm.nickname" style="width: 200px" />
            </el-form-item>
            <el-form-item label="让利" prop="difference">
              <el-input v-model.number="postForm.difference" style="width: 200px" />
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="postForm.address"/>
            </el-form-item>
            <el-form-item label="银行">
              <el-input v-model="postForm.bank_name"/>
            </el-form-item>
            <el-form-item label="卡号">
              <el-input v-model="postForm.bank_number"/>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import api from '@/api'
import Sticky from '@/components/Sticky'
import Warn from './Warn'
export default {
  name: 'NewsDetail',
  components: {
    Sticky,
    Warn
  },
  props: {
    /**
     * 是否为编辑
     */
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const mobileValidate = (rule, value, callback) => {
      if (value === undefined || value.length <= 0) {
        callback(new Error('请输入手机号码'))
      } else {
        if (!/^1[3456789]\d{9}$/.test(value)) {
          callback(new Error('手机号码错误'))
        } else {
          callback()
        }
      }
    }
    const differenceValidate = (rule, value, callback) => {
      if (Number.isNaN(+value) || value <= 0) {
        callback(new Error('让利错误'))
      } else {
        callback()
      }
    }
    const typeValidate = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请选择分类'))
      } else {
        callback()
      }
    }
    return {
      id: -1,
      loading: false,
      postForm: {
        color: 'draft',
        difference: 20,
        image: []
      },
      rules: {
        mobile: [{ validator: mobileValidate }],
        difference: [{ validator: differenceValidate }],
        type: [{ validator: typeValidate }]
      },
      typeList: []
    }
  },
  created() {
    this.getTypeList()
    /**
     * 获取编辑数据
     */
    if (this.isEdit) {
      this.id = (this.$route.params && this.$route.params.id) || -1
      this.fetchData(this.id)
    }
  },
  methods: {
    /**
     * 分类
     */
    getTypeList() {
      api.merchantType.list({ export: true }).then(response => {
        this.typeList = response.data
      })
    },
    /**
     * 获取编辑数据
     */
    async fetchData(id) {
      const response = await api.merchant.view({ id })
      this.postForm = Object.assign({}, response.data)
      // 状态
      this.postForm.status = +this.postForm.status === 1
      // 图片
      if (this.postForm.image) {
        this.postForm.image = [{
          id: +this.postForm.image.id,
          url: this.postForm.image.thumbnail_path
        }]
      } else {
        this.postForm.image = []
      }
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 状态
          const status = this.postForm.status ? 1 : 0
          // 图片
          let image = 0
          if (this.postForm.image && this.postForm.image.length > 0) {
            image = this.postForm.image.map(x => x.id)[0]
          }
          api.merchant.edit({
            id: this.id,
            ...this.postForm,
            status,
            image_id: image
          }).then(response => {
            this.loading = false
            this.$notify({
              title: '成功',
              message: response.message || '操作成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push(`/merchant/edit/${response.data.id}`)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    /**
     * 图片上传
     */
    onSuccess(data, fileList) {
      if (data && data.id) {
        this.postForm.image.push({
          id: data.id,
          url: data.thumbnail
        })
      }
    },
    /**
     * 删除图片
     */
    onRemove(data, fileList) {
      if (data && data.id) {
        this.postForm.image = this.postForm.image.filter(x => x.id !== data.id)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  //
}
</style>
